package com.example.w03;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    private TextView result;
    private EditText name;
    private EditText number;
    private RadioGroup radioGroup;
    private String results;
    private String hobbys;
    private Button register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name = findViewById(R.id.name1);
        number = findViewById(R.id.number1);
        radioGroup = findViewById(R.id.sex);
        CheckBox sing = findViewById(R.id.sing);
        CheckBox basketball = findViewById(R.id.basketball);
        CheckBox running = findViewById(R.id.running);
        register = findViewById(R.id.register);

        sing.setOnCheckedChangeListener(this);
        basketball.setOnCheckedChangeListener(this);
        running.setOnCheckedChangeListener(this);

        result = findViewById(R.id.result);
        hobbys = new String();

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                String sing1=sing.toString();
//                String basketball1=basketball.toString();
//                String running1=running.toString();
//                Editable name1=name.getText();
//                Editable number1=number.getText();
//                String sex=radioGroup.toString();

                String sing1 = (String) sing.getText();
                String basketball1 = (String) basketball.getText();
                String running1 = (String) running.getText();
                Editable name1 = name.getText();
                Editable number1 = number.getText();
                String sex = "";
                for (int i = 0; i < radioGroup.getChildCount(); i++) {
                    RadioButton r = (RadioButton) radioGroup.getChildAt(i);
                    if (r.isChecked()) {
                        sex = r.getText().toString();
                    }
                }


                results = "您的学号是:" + number1 + "  您的姓名是:" + name1 + "  您的爱好是:" + sing1 + basketball1 + running1 + "您的性别是:" + sex;
                System.out.println(results);
                result.setText(results);
            }
        });

    }


    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String motion=compoundButton.getText().toString();
        if(b){
            if (!results.contains(motion)){
                hobbys=hobbys+motion;
            }
        }else{
            if(hobbys.contains(motion)){
                hobbys=hobbys.replace(motion,"");
            }
        }
    }
    }
